<?php
namespace app\api\controller;
use app\common\controller\Common;
use app\common\exception\FailException;
use app\common\exception\ParameterException;
use app\common\exception\UserException;
use app\common\logic\User as UserLogic;
use think\Request;
use think\Loader;


/**
 * @title 登录注册模块接口
 * @description 接口说明
 * @group 接口分组
 *
 */
class Login extends Common
{
    /**
     * @title 手机号登陆
     * @description 接口说明
     * @author WCJ
     * @url /api/login/mobileLogin
     * @method POST
     * @param name:mobile type:int require:1 default:1 other: desc:手机号
     * @param name:uid type:int require:1 default:1 other: desc:用户id
     * @param name:code type:int require:1 default:1 other: desc:验证码
     * @param name:type type:int require:1 default:1 other: desc:状态(1.会员登陆，2.商家，3.代理商，4.省级代理商，5.内部员工)
     *
     * @return id: 用户ID
     * @return username: 用户名
     * @return nickname: 用户昵称
     * @return mobile: 手机号
     * @return qq: QQ
     * @return salt: 密码盐
     * @return sex: 性别（1：男，2：女）
     * @return password: 密码（不体现）
     * @return email: 电子邮箱
     * @return birthday: 生日
     * @return reg_time: 注册时间
     * @return last_time: 最后一次登录时间
     * @return last_ip: 最后一次登录IP
     * @return pay_password: 支付密码（不体现）
     * @return token: token验证值
     *
     */
    public function mobileLogin(Request $request)
    {
        if($request->isPost()) {
            $mobile = $request->param('mobile');
            $uid = $request->param('uid');
            $code = $request->param('code');
            $type = $request->param('type');

            $validate = Loader::validate("User");

            if (!$validate->scene("logincode")->check(['mobile' => $mobile, 'code' => $code])) {
                throw new ParameterException(['code' => 201, 'msg' => $validate->getError()]);
                die();
            }

            if (!$validate->scene("uid")->check(['user_id' => $uid])) {
                throw new ParameterException(['code' => 201, 'msg' => $validate->getError()]);
                die();
            }
            if (!$validate->scene("type")->check(['type' => $type])) {
                throw new ParameterException(['code' => 201, 'msg' => $validate->getError()]);
                die();
            }

            $ip = $request->ip();
            $userlogic = new UserLogic();
            $userinfo = $userlogic->mobilelogin($mobile,$code,$uid,$type,$ip);
            if($userinfo['code']!=200){
                throw new UserException($userinfo);
            }
            return $userinfo;

        }
    }

    /**
     * @title 账号登陆
     * @description 接口说明
     * @author WCJ
     * @url /api/login/userLogin
     * @method POST
     * @param name:username type:int require:1 default:1 other: desc:用户名
     * @param name:password type:int require:1 default:1 other: desc:密码
     * @param name:type type:int require:1 default:1 other: desc:状态(1.会员登陆，2.商家，3.代理商，4.省级代理商，5.内部员工)
     *
     * @return id: 用户ID
     * @return username: 用户名
     * @return nickname: 用户昵称
     * @return mobile: 手机号
     * @return qq: QQ
     * @return salt: 密码盐
     * @return sex: 性别（1：男，2：女）
     * @return password: 密码（不体现）
     * @return email: 电子邮箱
     * @return birthday: 生日
     * @return reg_time: 注册时间
     * @return last_time: 最后一次登录时间
     * @return last_ip: 最后一次登录IP
     * @return pay_password: 支付密码（不体现）
     * @return token: token验证值
     *
     */
    public function userLogin(Request $request)
    {
        if($request->isPost()) {
            $username = $request->param('username');
            $password = $request->param('password');
            $type = $request->param('type');

            $validate = Loader::validate("User");

            if (!$validate->scene("login")->check(['username' => $username, 'password' => $password])) {
                throw new ParameterException(['code' => 201,'errorCode' => 400,  'msg' => $validate->getError()]);
                die();
            }

            if (!$validate->scene("type")->check(['type' => $type])) {
                throw new ParameterException(['code' => 201,'errorCode' => 400, 'msg' => $validate->getError()]);
                die();
            }

            $ip = $request->ip();
            $userlogic = new UserLogic();
            $password=md5($password);
            $userinfo = $userlogic->userlogin($username,$password,$type,$ip);
            if($userinfo['code']!=200){
                throw new UserException($userinfo);
            }
            return $userinfo;

        }
    }

    /**
     * @title 获取手机号下的账户
     * @description 接口说明
     * @author WCJ
     * @url /api/login/getMoblieUser
     * @method POST
     * @param name:mobile type:int require:1 default:1 other: desc:手机号
     * @param name:type type:int require:1 default:1 other: desc:状态1.会员登陆，2.内部员工/代理/省级代理/商家登陆
     *
     * @return data: 用户信息
     *
     */
    public function getMoblieUser(Request $request)
    {
        if($request->isPost()) {
            $mobile = $request->param('mobile');
            $type = $request->param('type');

            $validate = Loader::validate("User");

            if (!$validate->scene("mobile")->check(['mobile' => $mobile])) {
                throw new ParameterException(['code' => 201,"msg"=>$validate->getError(),"errorCode"=>404]);

            }
            if (!$validate->scene("uid")->check(['user_id' => $type])) {
                throw new ParameterException(['code' => 201,"msg"=>$validate->getError(),"errorCode"=>404]);

            }

            if($type>2){
                throw new ParameterException(['code' => 201,"msg"=>"参数错误","errorCode"=>404]);
            }

            $userlogic = new UserLogic();
            $userdata = $userlogic->get_users($mobile,$type);
            if($userdata['code']!=200){
                throw new UserException($userdata);
            }
            return $userdata;

        }
    }

    /**
     * @title 注册接口
     * @description 接口说明
     * @author WCJ
     * @url /api/login/register
     * @method POST
     *
     * @param name:password type:string require:1 default:0 other: desc:密码
     * @param name:mobile type:int require:1 default:0 other: desc:手机号
     * @param name:referee type:int require:0 default:0 other: desc:推荐人ID
     * @param name:agent_id type:int require:0 default:0 other: desc:总代理商ID
     *
     * @return id: 用户ID
     * @return username: 用户名
     * @return nickname: 用户昵称
     * @return mobile: 手机号
     * @return qq: QQ
     * @return salt: 密码盐
     * @return sex: 性别（1：男，2：女）
     * @return password: 密码（不体现）
     * @return email: 电子邮箱
     * @return birthday: 生日
     * @return reg_time: 注册时间
     * @return last_time: 最后一次登录时间
     * @return last_ip: 最后一次登录IP
     * @return pay_password: 支付密码（不体现）
     * @return token: token验证值
     *
     */
    public function register(Request $request)
    {
        if ($request->isPost()) {

            $referee_id = intval($request->param('referee'));
            $top_agent_id = intval($request->param('agent_id'));
            $password = $request->param('password');
            $mobile = $request->param('mobile');

            $validate = Loader::validate("User");
            if (!$validate->scene("register")->check(['mobile' => $mobile, 'password' => $password,'agent_id'=>$top_agent_id,'referee_id'=>$referee_id])) {

                throw new ParameterException(['code' => 201, 'msg' => $validate->getError(),"errorCode"=>404]);
                die();
            }

            $userlogic = new UserLogic();
            $password=md5($password);
            $data = $userlogic->add_user($password,$mobile,$referee_id,$top_agent_id);


            if ($data) {
                $data['pay_password'] = "******";
                $data['password'] = "******";
                return ['code' => 200, 'msg' => '注册成功', 'data' => $data];
            } else {
                throw new FailException(['code' => 201, 'msg' => '注册失败!',"errorCode"=>404]);
                die();
            }
        }
    }


}